package com.hujiang.demo.utils;

import android.content.Context;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class Config {

    private static String TAG = "Config";

    private String envName;

    private static Config config;

    private Config(InputStream configIS) {
        Properties configP = new Properties();
        try {
            configP.load(configIS);
            envName = configP.getProperty("envName").toLowerCase();
        } catch (IOException e) {
            throw new RuntimeException("加载配置文件失败");
        }

    }

    public synchronized static Config getInstance(Context context) {
        if (config == null) {
            InputStream configIS = null;
            try {
                configIS = context.getAssets().open("config.properties");
                config = new Config(configIS);
            } catch (IOException e) {
                Log.e(TAG, e.toString());
            } finally {
                if (configIS != null) {
                    try {
                        configIS.close();
                    } catch (IOException e) {
                    }
                }
            }

        }
        return config;
    }

    public String getEnvName() {
        try {
            return new String(envName.getBytes("ISO-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return "";
    }
}
